<?php
/**
 * 功能描述：	用户管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_UserServe extends Model {

    /**
     * Notes:列表导出
     * User: ZhuHaili
     */
    function getEssayLists($mouldCode, $menu_id, $pageNo, $fid, $idStr, $search, $orderby){
        // 查询条件
        $searchInfo = SYS_SESSIONS_GET($mouldCode.'_search_info');
        $whereAdd = $searchInfo ? $searchInfo : array();
        if(!empty($search) && is_array($search)){
            $whereAdd = array();
            foreach($search as $k=>$v){
                if($k == 'search_title' && $v){
                    $whereAdd['locate']['a.title'] = $v;
                }else if($k == 'search_status' && $v){
                    $whereAdd['where']['a.status'] = $v;
                }else if($k == 'search_startdate' && $v){
                    $whereAdd['gts']['a.createdate'] = $v;
                }else if($k == 'search_enddate' && $v){
                    $whereAdd['lts']['a.createdate'] = $v;
                }
            }
            SYS_SESSIONS_SET($mouldCode.'_search_info', $whereAdd);
        }
        if($idStr){
            $whereAdd['in']['a.id'] = $idStr;
        }
        $essayM = new Admin_EssayModel();
        $start = 0;
        $limit = 0;
        if($pageNo){
            // 分页条数
            $limit = 20;
            if(isset($GLOBALS['ADMIN']['sys_pages']) && intval($GLOBALS['ADMIN']['sys_pages'])){
                $limit = intval($GLOBALS['ADMIN']['sys_pages']);
            }
            // 计算分页
            $counts = $essayM->gerEssayCount($mouldCode, $menu_id, $fid, $whereAdd);
            $pageSum = $counts ? ceil($counts/$limit) : 1;    // 总页数
            $pageSum = $pageSum > 1 ? $pageSum : 1;
            $pageNo = $pageNo > $pageSum ? $pageSum : $pageNo; // 最大页数==总页数
            $start = $pageNo * $limit - $limit;
        }

        // 获取自定义的字段标识和注释名字
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '1');
        $fields = $fields1;
        //$fields2 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '2');
        //$fields = array_merge($fields1, $fields2);

        $thList['title'] = '帐号';
        $thList['mobile'] = '手机';
        $thList['email'] = '邮箱';
        $thList['levelstxt'] = '等级';
        $thList['status'] = '状态';
        $thList['statustxt'] = '状态描述';
        $thList['createdate'] = '创建时间';
        if(!empty($fields)){
            foreach($fields as $f){
                $thList[$f['fieldcode']] = $f['fieldname'];
            }
        }
        $essayM = new Admin_EssayModel();
        $trList = $essayM->getEssayLists($mouldCode, $menu_id, $fid, $whereAdd, $start, $limit, $orderby);
        $statusArr = BaseData::$statusArr;
        $levelsArr = BaseData::$userLevelArr;
        foreach($trList as $k=>$v){
            $trList[$k] = $v;
            $trList[$k]['levelstxt'] = isset($levelsArr[$v['levels']]) ? $levelsArr[$v['levels']] : $v['levels'];
            $trList[$k]['statustxt'] = isset($statusArr[$v['status']]) ? $statusArr[$v['status']] : $v['status'];
        }
        return array('thList'=>$thList,'trList'=>$trList, 'page'=>$pageNo);
    }

    /**
     * Notes:添加 / 修改
     * User: ZHL
     * Date: 2019/9/22
     * @param $param
     * @return string
     */
    function setEssay($param, $setFlag){
        // 参数验证
        if(!in_array($setFlag, array('add', 'update'))){return '非法请求_未指定操作类型';} // 类型是否add和update
        if($setFlag == 'update'){
            if(!isset($param['id']) || !intval($param['id'])){
                return '非法请求_未指文章ID';
            }
            $id = intval($param['id']);
        }
        unset($param['subFlag']);
        $mouldCode = $param['mould_code'];
        $menu_id = intval($param['menu_id']);
        if(!$mouldCode){return '非法请求_未指定模型';}
        if(!$menu_id){return '非法请求_未指定菜单';}
        if(!isset($param['title']) || !$param['title']){return '帐号不能为空';}
        if(!isset($param['power_id']) || !$param['power_id']){return '角色不能为空';}
        // 获取开放字段
        $fieldSer = new Admin_FieldServe();
        $fields1 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '1');
        $fields2 = $fieldSer->getOpenClassFieldsInfos($menu_id, '2', '2');
        // 主表匹配插入的数据
        $essSer = new Admin_EssayServe();
        $data = $essSer->getParamMateFieldData($fields1, $param);
        $data = !empty($data) && is_array($data) ? array_filter($data) : array();

        $data['menu_id'] = $menu_id;
        $data['title'] = $param['title'];
        $data['fid'] = isset($param['fid']) ? intval($param['fid']) : 0;
        $data['status'] = isset($param['status']) ? intval($param['status']) : 1;
        $data['viewseq'] = isset($param['viewseq']) ? intval($param['viewseq']) : 0;
        $data['levels'] = isset($param['levels']) ? intval($param['levels']) : 3;
        $data['headimg'] = isset($param['headimg']) ? $param['headimg'] : '';
        $data['createdate'] = date('Y-m-d H:i:s');
        if($setFlag == 'add'){
            if(!isset($param['pwd']) || !$param['pwd']){return '密码不能为空';}
            $data['pwd'] = setPwd($param['pwd']);
        }
        if($setFlag == 'update'){
            if(isset($param['pwd']) && $param['pwd']){$data['pwd'] = setPwd($param['pwd']);}
        }
        $data['mobile'] = isset($param['mobile']) ? $param['mobile'] : '';
        $data['email'] = isset($param['email']) ? $param['email'] : '';
        $data['power_id'] = isset($param['power_id']) ? $param['power_id'] : '';

        if(isset($param['createdate']) && $param['createdate']){
            $data['createdate'] = date("Y-m-d H:i:s", strtotime($param['createdate']));
        }
        // 主表数据操作
        $db = $this->conn('mysql');
        if($setFlag == 'add'){
            $id = $db->addData($mouldCode, $data, 'id');
            if(!$id){ return '添加主数据失败';}
        }else if($setFlag == 'update'){
            $mainRes = $db->updateData($mouldCode, $data, array('id'=>$id));
            if(!$mainRes){ return '修改主数据失败';}
        }
        // 匹配副表数据
        $dataDetail = $essSer->getParamMateFieldData($fields2, $param);
        $dataDetail = !empty($dataDetail) && is_array($dataDetail) ? array_filter($dataDetail) : array();
        $dataDetail['essay_id'] = $id;
        if($setFlag == 'add'){
            $viceResult = $db->addData($mouldCode.'_detail', $dataDetail);  // 插入副表数据
        }else if($setFlag == 'update'){
            $viceResult = $db->updateData($mouldCode.'_detail', $dataDetail, array('essay_id'=>$id));  // 修改副表数据
        }
        if(!$viceResult){ return '详情数据操作失败';}
        // 开启了多个菜单关联文章
        if($GLOBALS['ADMIN']['classmore_flag'] == 1){
            $mouldSer = new Admin_MouldServe();
            if($mouldSer->verifyTable($mouldCode.'_class')){
                $dataClass['menu_id'] = $menu_id;
                $dataClass['essay_id'] = $id;
                if($setFlag == 'update'){ $db->delData($mouldCode.'_class', array('essay_id'=>$id));} // 修改时先删除旧数据
                $db->addData($mouldCode.'_class', $dataClass); // 默认添加一个自己菜单
                if(isset($param['essay_menu_mate']) && $param['essay_menu_mate']){
                    $menu_idArr = explode(',',$param['essay_menu_mate']);
                    foreach($menu_idArr as $k=>$menu_idMate){
                        if($menu_id != $menu_idMate){
                            $dataClass['menu_id'] = $menu_idMate;
                            $db->addData($mouldCode.'_class', $dataClass);
                        }
                    }
                }
            }
        }
        return 'Y';
    }

}